package com.commerce.recording.activity;

import android.app.AlertDialog;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.commerce.recording.R;

public class SettingActivity extends AppCompatActivity {

    private TextView settingTvFinish;
    private Switch settingSthName;
    private RelativeLayout settingTvQuality;
    private RelativeLayout settingTvFormat;
    private SharedPreferences sp_name_audio;
    private TextView mSettingTvQualitys;
    private TextView mSettingTvFormats;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        sp_name_audio = getSharedPreferences("sp_name_audio", MODE_PRIVATE);
        initViews();
    }

    private void initViews() {
        settingTvFinish = findViewById(R.id.setting_tv_finish);
        settingTvFinish.setOnClickListener((v -> finish()));
        settingSthName = findViewById(R.id.setting_sth_name);
        settingSthName.setChecked(sp_name_audio.getBoolean("Dialog", false));
        mSettingTvQualitys = findViewById(R.id.setting_tv_qualitys);
        mSettingTvFormats = findViewById(R.id.setting_tv_formats);
        settingTvQuality = findViewById(R.id.setting_tv_quality);
        settingTvFormat = findViewById(R.id.setting_tv_format);
        setData();
    }

    private void setData() {
        //获取设置数据
        int anIntQuality = sp_name_audio.getInt("BitRate", 1);
        int[] intArrayQuality = getResources().getIntArray(R.array.format);
        String[] stringArrayQuality = getResources().getStringArray(R.array.format_name);
        int anIntFormat = sp_name_audio.getInt("Suffix", 1);
        String[] stringArrayFormat = getResources().getStringArray(R.array.format_suffix);
        mSettingTvQualitys.setText(stringArrayQuality[anIntQuality]);
        mSettingTvFormats.setText(stringArrayFormat[anIntFormat]);
        settingTvQuality.setOnClickListener((v -> {
            show(stringArrayQuality,anIntQuality,intArrayQuality);
        }));
        settingTvFormat.setOnClickListener((v -> {
            show(stringArrayFormat,anIntFormat, null);
        }));
    }

    private void show(String[] stringArray, int i, int[] intArray) {
        TextView[] settingTvText = new TextView[3];
        AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
        View view = LayoutInflater.from(SettingActivity.this).inflate(R.layout.setting_dailog, null);
        settingTvText[0] = view.findViewById(R.id.setting_tv_text1);
        settingTvText[1] = view.findViewById(R.id.setting_tv_text2);
        settingTvText[2] = view.findViewById(R.id.setting_tv_text3);
        settingTvText[i].setBackgroundColor(Color.argb(200,0,191,255));
        builder.setView(view);
        AlertDialog alertDialog = builder.create();
        for (int a=0;a<settingTvText.length;a++){
            settingTvText[a].setText(stringArray[a]);
            int finalA = a;
            settingTvText[a].setOnClickListener((v ->{
                if(intArray!=null) {
                    sp_name_audio.edit().putInt("BitRate", finalA).apply();
                }else {
                    sp_name_audio.edit().putInt("Suffix", finalA).apply();
                }
                setData();
                alertDialog.cancel();
            }));
        }
        alertDialog.show();

    }


    @Override
    protected void onStop() {
        sp_name_audio.edit().putBoolean("Dialog", settingSthName.isChecked()).apply();
        super.onStop();
    }
}